package com.aote.component.replacer;

import java.util.HashMap;
import java.util.Map;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.browsesoft.PropertiesService;

public class ReplacerFactory {
	/**
	 * 单例
	 */
	private static ReplacerFactory self = new ReplacerFactory();

	/**
	 * 缓存
	 * 
	 */
	private Map replacers = new HashMap();

	private ReplacerFactory() {
		this.load();
	}

	/**
	 * 
	 * @return
	 */
	public static ReplacerFactory getInstance() {
		return self;
	}

	/**
	 * 装载默认值处理器
	 * 
	 */
	private void load() {
		try {
			NodeList list = PropertiesService.getDocument()
					.getElementsByTagName("replacer");
			// 取得配置
			for (int i = 0; i < list.getLength(); i++) {
				// 如果是元素,// 得到标记名和类名

				Element element = (Element) list.item(i);
				String tag = element.getAttribute("name");
				String className = element.getAttribute("className");
				Replacer processor = (Replacer) Class.forName(className)
						.newInstance();
				replacers.put(tag, processor);

			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * 
	 */
	public Replacer getReplacer(String replacerName) {
		Replacer result = (Replacer) replacers.get(replacerName);
		return result;
	}

}
